package com.md24housecall.adapter;

import java.util.List;
import org.json.JSONObject;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;
import com.md24housecall.activity.R;
import com.md24housecall.model.UserAccountIHealth;
import com.md24housecall.util.GlobalConstant;

public class MultiIHealthAdapter extends BaseAdapter {
	private List<UserAccountIHealth> listUser;
	private Context mContext;
	private OnDeleteListener listener;
	private boolean hasButtonDelete=true;
	
	public void setOnDeleteListener(OnDeleteListener listener){
		this.listener=listener;
	}
	
	public MultiIHealthAdapter(Context context,List<UserAccountIHealth> listUser){
		this.listUser=listUser;
		mContext=context;
	}
	
	public void setButtonDelete(boolean hasButton){
		hasButtonDelete=hasButton;
	}
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return listUser.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@SuppressLint("InflateParams")
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		View rowView = convertView;
		ViewHolder viewHolder;
		if (rowView == null) {
			LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			rowView = inflater.inflate(R.layout.ihealth_account_item, null);
			viewHolder = new ViewHolder();
			viewHolder.txvOrdinalNumber = (TextView) rowView.findViewById(R.id.txvOrdinalNumber);
			viewHolder.txvEmail = (TextView) rowView.findViewById(R.id.txvEmail);
			viewHolder.btnDelete=(Button)rowView.findViewById(R.id.btnDelete);
			rowView.setTag(viewHolder);
		}
		if(position%2==0){
			rowView.setBackgroundColor(Color.parseColor("#b2d4eb"));
		}else{
			rowView.setBackgroundColor(Color.parseColor("#33b5e5"));
		}
		viewHolder = (ViewHolder) rowView.getTag();
		viewHolder.btnDelete.setTag(position);
		if(!hasButtonDelete){
			viewHolder.btnDelete.setVisibility(View.GONE);
			viewHolder.txvOrdinalNumber.setVisibility(View.GONE);
		}
		viewHolder.btnDelete.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(final View v) {
				// TODO Auto-generated method stub
				if(listener!=null){
					int index=0;
					try{
						index=Integer.parseInt(v.getTag().toString());
						listener.onDeleteComplete(index);
					}catch(Exception e){
						e.printStackTrace();
					}
				}
			}
		});
		viewHolder.txvOrdinalNumber.setText(""+(position+1));
		String name="";
		try {
			JSONObject object=new JSONObject(listUser.get(position).getUserInfo());
			name=object.getString("nickname");
			if(name.equals("null"))
				name="";
		} catch (Exception e) {
			// TODO: handle exception
		}
		android.util.Log.d(GlobalConstant.TAG,"name ihealth: "+name);
		viewHolder.txvEmail.setText(name);
		return rowView;
	}
	
	public static class ViewHolder {
		public TextView txvOrdinalNumber;
		public TextView txvEmail;
		public Button btnDelete;
	}

	public interface OnDeleteListener{
		public void onDeleteComplete(int position);
	}
}
